package com.hyj.ecp.backstage.cust.service.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.SqlSession;



public class PaginationHelper {

 

    public static int getRowCount(SqlSession sqlSession, String statementName, Object values) {

       int count = 0;

       try {
           MappedStatement mst = sqlSession.getConfiguration().getMappedStatement(statementName);

           BoundSql boundSql = mst.getBoundSql(values);
           
           String sql = " select count(1)  from (" + boundSql.getSql() + ") t ";

           PreparedStatement pstmt = sqlSession.getConnection().prepareStatement(sql);
           ResultSet rs = pstmt.executeQuery();

           if (rs.next()) {

              count = rs.getInt(1);

           }
           rs.close();

           pstmt.close();

       } catch (Exception e) {

           count = 0;

           throw new RuntimeException(e);

       }

       return count;

    }
    }

